Skip to contentMethod: lambda$runSafelyAndWait$0(AtomicReference, Callable, AtomicReference, CountDownLatch)
1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * SteelBlue: DCI User Interfaces
5: * http://tidalwave.it/projects/steelblue
6: *
7: * Copyright (C) 2015 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/steelblue-src
22: * git clone https://github.com/tidalwave-it/steelblue-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.ui.javafx.impl.util;
27:
28: import jakarta.annotation.Nonnull;
29: import java.util.concurrent.Callable;
30: import java.util.concurrent.CountDownLatch;
31: import java.util.concurrent.TimeUnit;
32: import java.util.concurrent.atomic.AtomicReference;
33: import javafx.application.Platform;
34: import it.tidalwave.ui.javafx.impl.DefaultNodeAndDelegate;
35: import lombok.SneakyThrows;
36: import lombok.experimental.UtilityClass;
37: import lombok.extern.slf4j.Slf4j;
38:
39: /***************************************************************************************************************************************************************
40: *
41: * @author Fabrizio Giudici
42: *
43: **************************************************************************************************************************************************************/
44: @UtilityClass @Slf4j
45: public final class JavaFXSafeRunner
46: {
47: private static final String P_TIMEOUT = DefaultNodeAndDelegate.class.getName() + ".initTimeout";
48: private static final int INITIALIZER_TIMEOUT = Integer.getInteger(P_TIMEOUT, 10);
49:
50: /***********************************************************************************************************************************************************
51: * {@return the value provided by the given {@link Callable}, running it in the JavaFX thread}.
52: * @param callable the callable
53: **********************************************************************************************************************************************************/
54: @SneakyThrows
55: public static <T> T runSafelyAndWait (@Nonnull final Callable<T> callable)
56: throws Exception
57: {
58: if (Platform.isFxApplicationThread())
59: {
60: return callable.call();
61: }
62:
63: final var latch = new CountDownLatch(1);
64: final var result = new AtomicReference<T>();
65: final var exception = new AtomicReference<Throwable>();
66: final var timeoutMessage = String.format("Likely deadlock in the JavaFX Thread. If you need longer time with the debugger, set -D%s=<v> (current : %s)",
67: P_TIMEOUT, INITIALIZER_TIMEOUT);
68: Platform.runLater(() ->
69: {
70: try
71: {
72: result.set(callable.call());
73: }
74: catch (Throwable e)
75: {
76: exception.set(e);
77: }
78: finally
79: {
80: latch.countDown();
81: }
82: });
83:
84: try
85: {
86: log.debug("Waiting for JavaFX thread...");
87:
88: if (!latch.await(INITIALIZER_TIMEOUT, TimeUnit.SECONDS))
89: {
90: throw new RuntimeException(timeoutMessage);
91: }
92: }
93: catch (InterruptedException e)
94: {
95: log.info("Interrupted");
96: Thread.currentThread().interrupt();
97: throw new RuntimeException(e);
98: }
99:
100: if (exception.get() != null)
101: {
102: throw exception.get();
103: }
104:
105: return result.get();
106: }
107: }